<template>
  <div class="home-search">
    <div class="homelocaltion">
      <div class="city" @click="toCity">
        <span>{{ currentCity.cityName }}</span>
      </div>
      <div class="localtion" @click="localtionClick">
        <span>我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>

    <!-- 日期选择 -->
    <div class="section" @click="show = true">
      <div class="start">
        <span class="text">入住</span>
        <span class="day">{{ startTime }}</span>
      </div>
      <div class="stay">共 {{ stayCount }} 晚</div>
      <div class="end">
        <span class="text">离店</span>
        <span class="day">{{ endTime }}</span>
      </div>
    </div>
    <van-calendar
      v-model:show="show"
      type="range"
      @confirm="onConfirm"
      color=" #ff9854"
    />

    <!-- 价格/人数 -->
    <div class="count">
      <span class="left">价格不限</span>
      <span class="right">人数不限</span>
    </div>

    <!-- 关键字 -->
    <div class="keywords">
      <span>关键字 / 位置 / 民宿名</span>
    </div>

    <!-- 搜索建议 -->
    <div class="advice">
      <template v-for="(item, index) in homeHotSuggets">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 开始搜索 -->
    <div class="search">
      <div class="text" @click="searchBtn">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router"

const router = useRouter()

// 点击我的位置，获取地理位置
const localtionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("获取位置成功", res)
    },
    (err) => {
      console.log("获取位置失败", err)
    },
    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    }
  )
}

// 点击城市，切换到 city 页面
const toCity = () => {
  router.push("/city")
}

// 获取对应的城市名称
import { useCityStore } from "@/stores/modules/city"
import { storeToRefs } from "pinia"

const cityStore = useCityStore()
const { currentCity } = storeToRefs(cityStore)

// 日期相关
import { ref, computed } from "vue"
import { formatMonthDay, getDiffDays } from "@/utils/format_date"
import { useMainStore } from "@/stores/modules/main"

const show = ref(false)
const mainStore = useMainStore()
const { startData, endDate } = storeToRefs(mainStore)
// const nowtime = new Date()
// const tomorrowTime = new Date()
// tomorrowTime.setDate(nowtime.getDate() + 1)

const startTime = computed(() => formatMonthDay(startData.value))
const endTime = computed(() => formatMonthDay(endDate.value))
const stayCount = ref(getDiffDays(startData.value, endDate.value))

const onConfirm = (value) => {
  startData.value = value[0]
  endDate.value = value[1]
  stayCount.value = getDiffDays(value[0], value[1])
  
  // 隐藏日历
  show.value = false
}

// 获取搜索建议
import { useHomeData } from "@/stores/modules/home"
const homeData = useHomeData()
homeData.fetchHotSuggestsData()
const { homeHotSuggets } = storeToRefs(homeData)

// 开始搜索
const searchBtn = () => {
  router.push({
    path: "/search",
    query: {
      startTime: startTime.value,
      endTime: endTime.value,
      currentCity: currentCity.value.cityName,
    },
  })
}
</script>

<style lang="less" scoped>
.homelocaltion {
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid #eee;
  .city {
    font-size: 14px;
  }
  .localtion {
    font-size: 14px;
    img {
      position: relative;
      top: -1px;
      margin-left: 5px;
      width: 18px;
    }
  }
}

.section {
  display: flex;
  justify-content: space-around;
  padding: 9px;
  border-bottom: 1px solid #eee;
  .start,
  .end {
    display: flex;
    flex: 1;
    flex-direction: column;
    margin-left: 10px;
    .text {
      font-size: 9px;
      color: #a0a1a4;
      font-weight: 400;
    }
    .day {
      padding-top: 3px;
      font-size: 15px;
      color: #333;
    }
  }

  .stay {
    flex: 1;
    display: flex;
    align-items: center;
    color: #a0a1a4;
    font-weight: 400;
    // justify-content: center;
  }
}

.count {
  display: flex;
  justify-content: space-between;
  padding: 11px;
  span {
    color: #a0a1a4;
    font-weight: 700;
  }
  .left {
    margin-left: 5px;
  }
  .right {
    margin-right: 37px;
  }
}

.keywords {
  margin-top: 6px;
  margin-left: 15px;
  color: #a0a1a4;
  font-weight: 700;
}

.advice {
  display: flex;
  flex-wrap: wrap;
  padding: 8px;
  margin-top: 4px;
  .item {
    margin: 4px;
    padding: 4px;
    border: 1px solid #fff;
    font-size: 13px;
    border-radius: 10px;
  }
}

.search {
  margin: 2px auto;
  width: 90%;
  height: 40px;
  background-image: var(--theme-linear-gradient);
  text-align: center;
  line-height: 40px;
  border: 1px solid #fff;
  border-radius: 20px;
  .text {
    font-weight: 700;
    color: #fff;
    font-size: 16px;
  }
}
</style>
